package com.asp.service.impl;

import com.asp.constant.MessageConstant;
import com.asp.context.BaseContext;
import com.asp.entity.TestResults;
import com.asp.entity.Topic;
import com.asp.exception.NotPermissionException;
import com.asp.mapper.PracticeMapper;
import com.asp.mapper.TestResultsMapper;
import com.asp.model.MyResults;
import com.asp.model.ResultDetial;
import com.asp.model.UserResults;
import com.asp.testMethods.FormatUtil;
import com.asp.service.ResultsService;
import com.hankcs.hanlp.HanLP;
import org.springframework.stereotype.Service;
import com.asp.testMethods.CosineSimilarity;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import java.util.ArrayList;

@Service
public class ResultsServiceImpl implements ResultsService {

    @Resource
    private TestResultsMapper testResultsMapper;

    @Resource
    private PracticeMapper practiceMapper;

    private CosineSimilarity cosineSimilarity;

    private FormatUtil formatUtil;

    @Override
    public List<MyResults> selectAllResults() {
        String userName = BaseContext.getCurrentUserName();
        List<TestResults> testResults = testResultsMapper.selectAllResultsByUserName(userName);
        List<MyResults> myResults = new ArrayList<>();
        for (int i = 0; i < testResults.size(); i++) {
            TestResults a = testResults.get(i);
            MyResults b = new MyResults();
            b.setNum(a.getNum());
            b.setId(a.getId());
            b.setTitle(a.getTestTitle());
            b.setName(a.getTestName());
            b.setResult(a.getResults);
            b.setImgUrl(a.getImgUrl());
            myResults.add(b);
        }
        return myResults;
    }

    @Override
    public ResultDetial selectTestByNum(Integer testNum) {
        TestResults testResult = testResultsMapper.selectTestByNum(testNum);
        String correctAnswer = practiceMapper.getCorrectAnswer(testResult.getId());
        ResultDetial resultDetial = new ResultDetial();
        resultDetial.setId(testResult.getId());
        resultDetial.setTitle(testResult.getTestTitle());
        resultDetial.setContent(testResult.getTestContent());
        resultDetial.setResult(testResult.getResults());
        resultDetial.setAnswer(testResult.getUserAnswer());
        resultDetial.setCorrectAnswer(correctAnswer);
        return resultDetial;
    }

    //提交答案
    @Override
    public String judgeTest(Integer testId,String userAnswer,String userName){
        String correctAnswer = practiceMapper.getCorrectAnswer(testId);
        UserResults userResults = new UserResults();
        userResults.setTestId(testId);
        userResults.setUserAnswer(userAnswer);
        userResults.setUserName(userName);
        double answerSimilarity = cosineSimilarity.getSimilarity(userAnswer,correctAnswer);
        System.out.println(answerSimilarity);
        if(answerSimilarity >= 0.6){
            userResults.setResults("right");
        }
        else{
            userResults.setResults("wrong");
        }
        testResultsMapper.addResults(userResults);
        testResultsMapper.updateResultsByTestId(testId);
        return userResults.getResults();
    }

    @Override
    public List<String> getTips(Integer id){
        try{
            String correctAnswer= practiceMapper.getCorrectAnswer(id);
            List<String> keywordList = HanLP.extractKeyword(formatUtil.RemovalOfStopWords(correctAnswer), 8);
            return keywordList;
        }catch(IOException e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void deleteResultsByNum(Integer testNum){
        testResultsMapper.deleteResultsByNum(testNum);
    }

    @Override
    public void deleteAllResults()
    {
        String userName = BaseContext.getCurrentUserName();
        testResultsMapper.deleteAllResults(userName);
    }


}